package com.example.project.controller;

import com.example.project.dao.SafetyTrainDao;
import com.example.project.dto.SafetyTrainBinDTO;
import com.example.project.dto.SafetyTrainBinPageDTO;
import com.example.project.dto.SafetyTrainPageDTO;
import com.example.project.entity.SafetyTrainBin;
import com.example.project.service.SafetyTrainBinService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (SafetyTrainBin)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@CrossOrigin
@RestController
@RequestMapping("safetyTrainBin")
public class SafetyTrainBinController {
    /**
     * 服务对象
     */
    @Resource
    private SafetyTrainBinService safetyTrainBinService;

    /**
     * 分页查询
     *
     * param safetyTrainBin 筛选条件
     * param pageRequest      分页对象
     * @return 查询结果
     */
    @PostMapping("findPage")
    public ResponseEntity<Page<SafetyTrainBinPageDTO>> queryByPage(@RequestBody SafetyTrainPageDTO data) throws ParseException {
        com.example.project.entity.Page page = data.getPage();
        Date end=null;
        Date star=null;
        if(data.getMyTime2().getEndTime()!=null&&data.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            end=format.parse(data.getMyTime2().getEndTime()+":00");
        }
        if(data.getMyTime2().getStartTime()!=null&&data.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            star =format.parse(data.getMyTime2().getStartTime()+":00");

        }
        return ResponseEntity.ok(this.safetyTrainBinService.queryByPage(data.getSafetyTrain(), PageRequest.of(page.getPage(),page.getSize()), star,end ));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @PostMapping("{id}")
    public ResponseEntity<SafetyTrainBin> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.safetyTrainBinService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param safetyTrainBin 实体
     * @return 新增结果
     */

    @PostMapping("add")
    public ResponseEntity<SafetyTrainBin> add(@RequestBody SafetyTrainBin safetyTrainBin) {
        return ResponseEntity.ok(this.safetyTrainBinService.insert(safetyTrainBin));
    }

    /**
     * 恢复功能
     *
     * @param safetyTrainBin 实体
     * @return 编辑结果
     */

    @PutMapping("restore")
    public ResponseEntity<Boolean> edit(@RequestBody SafetyTrainBin safetyTrainBin) {
        return ResponseEntity.ok(this.safetyTrainBinService.update1(safetyTrainBin));
    }

    @PutMapping("put")
    public ResponseEntity<SafetyTrainBin> edit1(@RequestBody SafetyTrainBin safetyTrainBin) {
        return ResponseEntity.ok(this.safetyTrainBinService.update(safetyTrainBin));
    }

    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */

    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody SafetyTrainBin data) {
        return ResponseEntity.ok(this.safetyTrainBinService.deleteById1(data.getId()));
    }

    /**
     * 批量删除
     * @param data id的字符串
     * @return 删除是否成功
     * */
     @DeleteMapping("delIds")
     public ResponseEntity<Boolean> deleteByIds(@RequestBody SafetyTrainBinDTO data) {
         return ResponseEntity.ok(this.safetyTrainBinService.deleteById2(data));
     }
    /**
     * 批量恢复
     * @param data id的字符串
     * @return 删除是否成功
     * */
    @PutMapping("putIds")
    public ResponseEntity<Boolean> editIds(@RequestBody SafetyTrainBinDTO data){
        return ResponseEntity.ok(this.safetyTrainBinService.update2(data));
    }
}

